#include "ringQueue.hpp"
#include <time.h>
#include <unistd.h>
#include <sys/types.h>

void *consumer(void *args)
{
    RingQueue<int> *rq = (RingQueue<int> *)args;
    while (true)
    {
        // 1、取任务
        int x;
        rq->pop(&x);
        // 2、处理数据，（消耗时间
        cout << "————消费：" << x << '——[' << pthread_self() << ']' << endl;
        sleep(1);
    }
}
void *productor(void *args)
{
    RingQueue<int> *rq = (RingQueue<int> *)args;
    while (true)
    {
        // 1、构造数据、任务（消耗时间
        int x = rand() % 100 + 1;
        // 2、入队
        rq->push(x);
        cout << "生产：" << x << '——[' << pthread_self() << ']' << endl;
    }
}
int main()
{
    srand((unsigned int)time(nullptr) ^ getpid());
    pthread_t c[3], p[2];
    RingQueue<int> *rq = new RingQueue<int>();
    pthread_create(c, nullptr, consumer, (void *)rq);
    pthread_create(c + 1, nullptr, consumer, (void *)rq);
    pthread_create(c + 2, nullptr, consumer, (void *)rq);

    pthread_create(p, nullptr, productor, (void *)rq);
    pthread_create(p + 1, nullptr, productor, (void *)rq);
    // rq->debug();

    for (int i = 0; i < 3; i++)
        pthread_join(c[i], nullptr);
    for (int i = 0; i < 2; i++)
        pthread_join(p[i], nullptr);
    return 0;
}